package com.flot.yazb.loading;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;

import com.flot.yazb.R;


/**
 * Created by Chexiangjia-MAC on 17/6/28.
 */
public class CLoadingDialog extends Dialog {
    public View itemView;

    public CLoadingDialog(Context context, int dialog) {
        super(context, dialog);
    }

    public static class Builder {
        private Context mContext;

        public Builder(Context mContext) {
            this.mContext = mContext;
        }

        public CLoadingDialog build() {
            View itemView = View.inflate(mContext, R.layout.dialog_base_progress, null);
            final CLoadingDialog dialog = new CLoadingDialog(mContext, R.style.Dialog);
            dialog.itemView = itemView;
            dialog.addContentView(itemView, new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT)
            );

            Window window = dialog.getWindow();
            WindowManager.LayoutParams params = window.getAttributes();
            params.dimAmount = 0f;
            window.setAttributes(params);

            dialog.setCanceledOnTouchOutside(false);

            return dialog;
        }
    }

    @Override
    public void show() {
        super.show();
        startRotateAnimation(itemView.findViewById(R.id.iv_loading_bg));
    }

    //旋转动画
    private static void startRotateAnimation(final View view) {
        RotateAnimation rotateAnimation = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        rotateAnimation.setDuration(1000);
        rotateAnimation.setRepeatCount(-1);
        LinearInterpolator lin = new LinearInterpolator();
        rotateAnimation.setInterpolator(lin);
        view.startAnimation(rotateAnimation);
    }
}
